#!usr/bin/env python3

import random,math

def logGet(idx,cur,maxNum,total,lst):
    print("第",idx,"个,红包领到",cur,"块,本次最多可能领到",maxNum,'块,剩余',total,'块随机列表',lst)
    
def randRedBag(total,totalNum,minNum,randomTime=3):
    randomTime+=1
    count = 0
    verVal = math.floor(total/totalNum)
    minNum = min(verVal,minNum) #每个红包最小值不能大于平均值
    maxGet = 0
    for i in range(totalNum):
        idx = i+1
        weight=0.18*(totalNum-idx) #设置权重,使得每次红包随机最大值更加接近
        trueMax = total-(totalNum-idx)*minNum
        maxNum = int(total-(totalNum-idx)*minNum*weight) #计算本次红包可能的最大值        
        maxNum = min(trueMax,maxNum)
        maxNum = max(minNum,maxNum)    
        #maxNum = random.randint(minNum,maxNum)
        lst = []
        if idx==totalNum:
            cur = total
            count+=cur
            logGet(idx,cur,maxNum,total,[cur]) #最后一个不需要随机
        else:            
            curMin = maxNum+1
            for x in range(1,randomTime):#这样更加随机
                n = random.randint(minNum,maxNum)
                curMin = min(curMin,n)
                lst.append(n)
            lst.sort()
            cur = curMin #lst[0]
            count+=cur
            total -= cur
            if total<minNum:
                logGet(idx,cur,maxNum,total,lst)
                print(minNum,maxNum,trueMax)
                raise ValueError("生成的红包小于最小限制值")
            logGet(idx,cur,maxNum,total,lst)
        
    print("总共领取了",count,"块")


for i in range(10):
    print("----------------------开始发红包------------------------i=",i+1)
    randRedBag(100,10,2)
    

            
        
